<?php /*
Copyright 2002 Dan Boitnott (dboitnot@yahoo.com)

This file is part of phpTasks.

phpTasks is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

phpTasks is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with phpTasks; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/ ?>
<?php require("common.php"); ?>
<?php
	// dump_hash($HTTP_VARS);
	if (!is_numeric($HTTP_VARS["taskid"])) {
		echo ("Invalid taskid! - 1");
		exit;
  	} else {
		$taskid = $HTTP_VARS["taskid"];
  	}

	if ($HTTP_VARS["action"]) {
		$completed = false;
		$red = "";
		switch ($HTTP_VARS["action"]) {
		case "Cancel":
			$red = "index.php";
			break;
   		case "Printable":
			$red = "taskedit.php?viewonly=true&taskid=" . $taskid;
		case "Add Log Entry":
			if ($red == "") {
				$entry = $HTTP_VARS["entry"];
				if ($entry && ($entry <> "")) {
					$sql = "INSERT INTO atasklog (taskid, userid, entry) VALUES (" . $taskid . ", " . $USER["userid"] . ", '" . $entry . "')";
					pg_exec($conn, $sql);
					$red = "taskedit.php?taskid=" . $taskid;
				} else {
					echo ("Invalid Log Entry");
					exit;
				}
    			}
		case "Update":
			if ($HTTP_VARS["completed"] == "on")
				$completed = true;
			else
				$completed = false;
			if ($taskid == 0) {
				$taskid = reserve_sequence($conn, "atasks_taskid_seq");
				$sql = "INSERT INTO atasks (taskid, priority, title, assigned_by, assigned_to, description, completed";
     				$sql .= ") VALUES (" . $taskid . ", " . $HTTP_VARS["priority"] . ", '" . $HTTP_VARS["title"] . "', " . $HTTP_VARS["assigned_by"] . ", " . $HTTP_VARS["assigned_to"] . ", '" . $HTTP_VARS["description"] . "'";
				if ($completed)
					$sql .= ", now()";
     				else
					$sql .= ", null";
				$sql .= ")";
			} else {
				$sql = "UPDATE atasks SET priority=" . $HTTP_VARS["priority"] . ", title='" . $HTTP_VARS["title"] . "', assigned_by=" . $HTTP_VARS["assigned_by"] . ", assigned_to=" . $HTTP_VARS["assigned_to"] . ", description='" . $HTTP_VARS["description"] . "'";
				if ($completed) {
					$sql .= ", completed=now()";
     				} else
					$sql .= ", completed=null";
				$sql .= " WHERE taskid=" . $taskid;
			}
			pg_exec($conn, $sql);
			if ($completed && $HTTP_VARS["assigned_by"] != $HTTP_VARS["assigned_to"]) {
				// Make sure the assigner has an e-mail address
				$m_sql = "SELECT email FROM ausers WHERE userid=" . $HTTP_VARS["assigned_by"];
				if($to_addr = get_resval($conn, $m_sql, "email")) {
					$subj = "Task #" . $HTTP_VARS["taskid"] . " completed.";
					$mesg = "Task #" . $HTTP_VARS["taskid"] . ": \"" . $HTTP_VARS["title"] . "\" was marked completed by " . $USER["name"] . ".\n\n";
					$mesg .= taskastext($conn, $HTTP_VARS["taskid"]);
					mail($to_addr, $subj, $mesg);
				}
			}
			if ($red == "")
				$red = "index.php";
			break;
   		default:
			echo ("Invalid action!");
			exit;
		}
		if ($red != "") {
			redirect($red);
			exit;
   		}
	} else {
		$hiddens = "<input type=hidden name='taskid' value='" . $taskid . "'>";
		if (is_numeric($taskid)) {
			if ($taskid == 0) {
				if ($USER["modifytasks"]) {
					$editable = true;
					$r["title"] = "New Task";
					$r["assigned_by"] = $USER["userid"];
					$r["assigned_to"] = $tl_userid;
					$r["priority"] = 5;
				} else {
					echo ("You are not authorized to create new tasks!");
					exit;
				}
			} else {
				if ($USER["modifytasks"]) {
					$editable = true;
     				} else {
					$editable = false;
     				}
				if ($r = fetchrow($conn, "tasks", "taskid=" . $taskid)) {
					if ((!$USER["viewalltasks"]) && ($r["assigned_to"] <> $USER["userid"])) {
						echo ("You are not authorized to view other's tasks!");
						exit;
      					}
				} else {
					echo ("Invalid taskid!");
					exit;
     				}
			}
		} else {
			echo ("Invalid taskid!");
			exit;
   		}
                if (isset($HTTP_VARS["viewonly"]))
                        $editable = false;
?>
<?php std_header("Task Editor"); ?>
<h1><?= ($taskid == 0) ? "New Task" : (($editable ? "Editing " : "") . "Task #" . $taskid) ?></h1>
<?php if ($editable) { ?>
	<form action=taskedit.php method=post>
	<?=$hiddens?>
<?php } ?>
<table border='0'>
	<tr>
		<td class=form_label nowrap>Title:</td>
		<td class=form_field>
			<?php if ($editable) { ?>
				<input type=text size=60 name='title' value='<?=html_encode($r["title"])?>'>
    			<?php } else { echo ($r["title"]); } ?>
		</td>
  	</tr><tr>
		<td class=form_label nowrap>Date:</td>
		<td class=form_field><?=$r["created"]?></td>
	</tr><tr>
		<td class=form_label nowrap>Assigned By:</td>
		<td class=form_field>
		<?php
			if ($editable) {
				std_select($conn, "assigned_by", "SELECT * FROM users ORDER BY name", "name", "userid", $r["assigned_by"]);
    			} else {
				$t = fetchrow($conn, "ausers", "userid=" . $r["assigned_by"]);
				echo ($t["name"]);
			}
   		?>
		</td>
  	</tr><tr>
		<td class=form_label nowrap>Completed:</td>
		<td class=form_field>
		<?php
			if ($editable) {
				echo ("<input type=checkbox name='completed' value='on'");
				if ($r["completed"])
					echo (" checked>" . $r["completed"]);
     				else
					echo (">");
   			} else {
				if ($r["completed"])
					echo ($r["completed"]);
     				else
					echo ("Incomplete");
			}
		?>
		</td>
	</tr><tr>
		<td class=form_label nowrap>Assigned To:</td>
		<td class=form_field>
		<?php
			if ($editable) {
				std_select($conn, "assigned_to", "SELECT * FROM users ORDER BY name", "name", "userid", $r["assigned_to"]);
			} else {
				$t = fetchrow($conn, "ausers", "userid=" . $r["assigned_to"]);
				echo ($t["name"]);
    			}
       		?>
		</td>
  	</tr><tr>
		<td class=form_label nowrap>Priority:</td>
		<td class=form_field>
		<?php
			if ($editable) {
				std_select($conn, "priority", "SELECT * FROM priorities ORDER BY num", "name", "num", $r["priority"]);
    			} else {
				$t = fetchrow($conn, "priorities", "num=" . $r["priority"]);
				echo ($t["name"]);
    			}
		?>
	</tr><tr>
		<td class=form_label nowrap>Description:</td>
		<td class=form_field>
		<?php
			if ($editable) {
				echo ("<textarea cols=60 name='description' rows=5 wrap=soft>" . $r["description"] . "</textarea>");
    			} else {
				echo ("<tt>" . html_encode($r["description"]) . "</tt>");
			}
		?>
		</td>
	</tr>
	<?php if ($editable) { ?>
		<tr valign='center'><td></td><td>
			<table border='0'><tr>
				<td><input type=submit name="action" value="Update"></td>
				<td>
					<input type=submit name="action" value="Cancel">
				</td>
                                <td>
					<input type=submit name="action" value="Printable">
				</td>
			</tr></table>
		</td>
	<?php } if ($taskid > 0) { ?>
	<tr>
		<td class=form_label>Log:</td>
		<td class=form_field>
		<?php
			$sql = "SELECT * FROM tasklog_list WHERE taskid=" . $taskid . " ORDER BY created";
			$res = pg_exec($conn, $sql);
			$rc = pg_numrows($res);
			if ($rc > 0) {
				echo ("<table border='0'>");
				for ($rnum = 0; $rnum < $rc; $rnum++) {
					$l = get_row($res, $rnum, "daydatetime");
					if ($rnum > 0)
						echo ("<tr><td></td></tr>");
					echo ("<tr class=log_header><td align='left'>" . $l["user"] . " - " . $l["created"] . "</td></tr>");
					echo ("<tr class=log_entry><td align='left'>" . html_encode($l["entry"]) . "</td></tr>");
				}
				echo ("</table>");
			} else
				echo ("No Entries");
			if ($editable) {
			?>
				<table border='0'><tr align='left'><td>
					<textarea cols=60 name='entry' rows=5 wrap=soft></textarea>
				</td></tr><tr align='right'><td>
					<input type=submit name="action" value="Add Log Entry">
   				</td></tr></table>
       			<?php
			}
			?>
		</td>
	</tr>
	<?php } ?>
</table>
</form>
<?php std_footer(); ?>
<?php } ?>